import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:musician_flutter/ui/provider/userRefresh.dart';

class UserScreen extends ConsumerStatefulWidget {
  @override
  _UserScreenState createState() => _UserScreenState();
}

class _UserScreenState extends ConsumerState<UserScreen> {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    ref.refresh(fetchUserProvider); // 进入页面时强制刷新数据
  }

  @override
  Widget build(BuildContext context) {
    final userAsync = ref.watch(fetchUserProvider); // 确保 UI 监听数据变化

    @override
    void didChangeDependencies() {
      super.didChangeDependencies();
      ref.refresh(fetchUserProvider); // 进入页面时强制刷新数据
    }

    return Scaffold(
      appBar: AppBar(title: const Text("用户信息")),
      body: Center(
        child: userAsync.when(
          data: (data) => Text("用户: $data"),
          loading: () => const CircularProgressIndicator(),
          error: (err, stack) => Text("加载失败: $err"),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          // 强制重新拉取数据
          await ref.refresh(fetchUserProvider);
        },
        child: const Icon(Icons.refresh),
      ),
    );
  }
}
